#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time   : 2020/7/13 21:46
# @Author : shi
# @Project: tableWidget
# @File   : freezeTableWidget.py


import typing

import qstring
from PySide2 import QtCore, QtGui
from PySide2.QtWidgets import QTableView, QAbstractItemView, QHeaderView, QAbstractSlider, QWidget, QLabel, QVBoxLayout
from PySide2.QtCore import Qt

from robotController import share


class FreezeTableView(QTableView, QAbstractSlider):

    # 初始化(model:列表数据，row_number:显示行数)
    def __init__(self, name="FreezeTableView", row_number=1,column_number=1, parent=None):
        super(FreezeTableView, self).__init__(parent)
        # 设置组件名称
        self.setObjectName(name)
        # 设置表头行数
        self.row_number = row_number
        self.column_number = column_number
        self.horizontalView = QTableView(self)
        self.frozenTableView = QTableView(self)
        self.BRTableView = QTableView(self)
        self.up = True

    # 设置数据
    def set_model(self, model):
        self.setModel(model)
        self.horizontalView.setModel(model)
        self.frozenTableView.setModel(model)
        self.BRTableView.setModel(model)
        [self.horizontalView.setRowHidden(row, True) for row in range(self.row_number, model.rowCount())]
        [self.BRTableView.setRowHidden(row, True) for row in range(self.row_number, model.rowCount())]
        [self.frozenTableView.setColumnHidden(col, True) for col in range(self.column_number, model.columnCount())]
        [self.BRTableView.setColumnHidden(col, True) for col in range(self.column_number, model.columnCount())]
        for column in range(model.columnCount()):
            self.setColumnWidth(column, 110);
            self.horizontalView.setColumnWidth(column, 110);
            self.frozenTableView.setColumnWidth(column, 110);
            self.BRTableView.setColumnWidth(column, 110);


        self.verticalHeader().hide()
        self.BRTableView.verticalHeader().hide()
        self.BRTableView.horizontalHeader().hide()
        self.frozenTableView.horizontalHeader().hide()

    def init(self):
        self.frozenTableInit()
        self.horizontalViewInit()
        self.BRTableViewInit()
        self.setStyleSheet('QTableView { border: none;'
                           'background-color: #ffffff;'
                           'color:#000000;}'
                           'QTableView ::section{height: 30px;}')
        self.horizontalHeader().sectionResized.connect(self.updateSectionWidth)
        self.verticalHeader().sectionResized.connect(self.updateSectionHeight)
        self.verticalScrollBar().valueChanged.connect(self.vConnectFV)
        self.frozenTableView.verticalScrollBar().valueChanged.connect(self.fVConnectV)
        self.horizontalScrollBar().valueChanged.connect(self.hConnectH)

    def vConnectFV(self, a0: int):
        self.viewport().stackUnder(self.frozenTableView)
        self.frozenTableView.stackUnder(self.horizontalView)
        self.frozenTableView.verticalScrollBar().setValue(a0)

    def fVConnectV(self, a0: int):
        self.viewport().stackUnder(self.frozenTableView)
        self.frozenTableView.stackUnder(self.horizontalView)
        self.verticalScrollBar().setValue(a0)

    def hConnectH(self, a0: int):
        self.viewport().stackUnder(self.horizontalView)
        self.horizontalView.stackUnder(self.frozenTableView)
        self.frozenTableView.stackUnder(self.frozenTableView)
        self.horizontalView.horizontalScrollBar().setValue(a0)

    def BRTableViewInit(self):
        self.BRTableView.setStyleSheet('QTableView { border: none;'
                                           'background-color: #999;'
                                           'color:#ffffff;'
                                           'selection-background-color: #999}'
                                           'QTableView ::section{height: 30px;}')
        self.BRTableView.show()
        self.updateFrozenTableGeometry()
        self.BRTableView.setVerticalScrollMode(self.ScrollPerPixel)

    def frozenTableInit(self):
        self.frozenTableView.verticalHeader().hide()
        self.frozenTableView.horizontalHeader().setFixedHeight(self.horizontalHeader().height())

        self.frozenTableView.setStyleSheet('QTableView { border: none;'
                                           'background-color: #999;'
                                           'color:#ffffff;'
                                           'selection-background-color: #999}'
                                           'QTableView ::section{height: 30px;}')
        self.frozenTableView.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
        self.frozenTableView.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
        self.frozenTableView.show()

        self.updateFrozenTableGeometry()
        self.frozenTableView.setVerticalScrollMode(self.ScrollPerPixel)
        self.setVerticalScrollMode(self.ScrollPerPixel)
        self.setHorizontalScrollMode(self.ScrollPerPixel)

    def horizontalViewInit(self):
        self.horizontalView.horizontalHeader().hide()
        self.horizontalView.verticalHeader().setFixedWidth(self.verticalHeader().width())

        self.frozenTableView.stackUnder(self.horizontalView)
        self.horizontalView.setStyleSheet('QTableView { border: none;'
                                          'background-color: #999;'
                                          'color:#ffffff;'
                                          'selection-background-color: #999}'
                                          'QTableView ::section{height: 30px;}')
        self.horizontalView.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
        self.horizontalView.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
        self.horizontalView.show()

        self.updateFrozenTableGeometry()
        self.horizontalView.setHorizontalScrollMode(self.ScrollPerPixel)

    def updateFrozenTableGeometry(self):
        columnWidth = 0
        for index in range(self.column_number):
            columnWidth = self.columnWidth(index)+columnWidth

        self.frozenTableView.setGeometry(self.verticalHeader().width() + self.frameWidth(),
                                         30,
                                         columnWidth,
                                         self.viewport().height() + self.horizontalHeader().height())

        self.horizontalView.setGeometry(self.frameWidth(),
                                        self.frameWidth() + self.horizontalHeader().height(),
                                        self.viewport().width() + self.verticalHeader().width(),
                                        self.rowHeight(0) * self.row_number)

        self.BRTableView.setGeometry(0,
                                     30,
                                     columnWidth,
                                     self.rowHeight(0) * self.row_number)

    def updateSectionWidth(self, logicalIndex, oldSize, newSize):
        self.horizontalView.setColumnWidth(logicalIndex, newSize)
        self.frozenTableView.setColumnWidth(logicalIndex, newSize)
        self.BRTableView.setColumnWidth(logicalIndex, newSize)
        self.updateFrozenTableGeometry()

    def updateSectionHeight(self, logicalIndex, oldSize, newSize):
        self.frozenTableView.setRowHeight(logicalIndex, newSize)
        if not logicalIndex:
            self.horizontalView.setRowHeight(0, newSize)
        self.updateFrozenTableGeometry()

    def resizeEvent(self, e: QtGui.QResizeEvent) -> None:
        QTableView.resizeEvent(self, e)
        self.updateFrozenTableGeometry()

    def scrollTo(self, index: QtCore.QModelIndex, hint: QAbstractItemView.ScrollHint = ...) -> None:
        if index.column() > 0 or index.row() > 0:
            QTableView.scrollTo(self, index, hint)

    def moveCursor(self, cursorAction: QAbstractItemView.CursorAction,
                   modifiers: typing.Union[QtCore.Qt.KeyboardModifiers,
                                           QtCore.Qt.KeyboardModifier]) -> QtCore.QModelIndex:
        current = QTableView.moveCursor(self, cursorAction, modifiers)
        if cursorAction == QAbstractItemView.MoveLeft and current.column() > 0 \
                and self.visualRect(current).topLeft().x() < self.frozenTableView.columnWidth(0):
            newValue = self.verticalScrollBar().value() + self.visualRect(current).topLeft().x() \
                       - self.frozenTableView.columnWidth(0)
            self.horizontalScrollBar().setValue(newValue)
        if cursorAction == QAbstractItemView.MoveUp and current.row() > 0 \
                and self.visualRect(current).topLeft().y() < self.horizontalView.rowHeight(0):
            newValue = self.horizontalScrollBar().value() + self.visualRect(current).topLeft().y() \
                       - self.horizontalView.rowHeight(0)
            self.verticalScrollBar().setValue(newValue)

        return current

class masking_element:
    def __init__(self):
        self.ui = share.SI.main.ui
        mpShadeWindow = QWidget(self.ui);
        label = QLabel(mpShadeWindow)
        label.setText("正在查询数据请稍后...")
        label.setStyleSheet('color:#ffffff;background-color:rgba(0,0,0,0.6);')
        label.setGeometry(self.ui.width()/2, self.ui.height()/2,label.width()+30 ,label.height() );
        mpShadeWindow.setStyleSheet("QWidget{background-color:rgba(0,0,0,0.6);}");
        mpShadeWindow.setGeometry(0, 0, self.ui.width(), self.ui.height());
        self.mpShadeWindow = mpShadeWindow

